package com.lelouch.wetlanddevice.service.impl;

import com.google.common.collect.Maps;
import com.lelouch.wetlandcommon.common.ServerResponse;
import com.lelouch.wetlanddevice.dao.CollectionDataMapper;
import com.lelouch.wetlanddevice.repository.CollectionDataRepository;
import com.lelouch.wetlanddevice.service.CollectionDataServive;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;

/**
 * @author lcy
 * @create 2018-09-12 15:07
 */
@Service
@Slf4j
public class CollectionDataServiceImpl implements CollectionDataServive {
    @Autowired
    private CollectionDataRepository collectionDataRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Value("${arcgis.server.ip}")
    private String arcgisServerIp;
    @Value("${arcgis.server.port}")
    private String arcgisServerPort;

    @Autowired
    private CollectionDataMapper collectionDataMapper;

    private final String COLLECTION = "CollectionData";

    @Override
    public ServerResponse<Integer> getCountBySiteIds(List<String> siteIds) {
        //if (CollectionUtils.isEmpty(siteIds)) {
        //    return ServerResponse.createBySuccess("查询成功！", collectionDataRepository.count());
        //}




        return ServerResponse.createBySuccess("查询成功!", collectionDataMapper.getCountBySiteIds(siteIds));
    }





//    @Override
//    public ServerResponse<Long> getCountBySiteIds(List<String> siteIds) {
//        if (CollectionUtils.isEmpty(siteIds)) {
//            return ServerResponse.createBySuccess("查询成功！", collectionDataRepository.count());
//        }
////        Aggregation aggregation = newAggregation(
////                group("coda_site").count().as("datanum"),
////                project("datanum").and("site").previousOperation()
////        );
////        AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, COLLECTION, Document.class);
////        List<Document> resultList = result.getMappedResults();
////
////
////        Map<String, Integer> map = Maps.newHashMap();
////        for (Document r : resultList) {
////            map.put(r.getString("site"),r.getInteger("datanum"));
////        }
//        Long count = 0L;
//        for (String siteid : siteIds) {
//            count = count + collectionDataRepository.countBySite(siteid);
//            //count=count+(map.get(siteid)==null?0:map.get(siteid));
//        }
//
//
//        return ServerResponse.createBySuccess("查询成功!", count);
//    }

    //public List<Document> getWetlandCountsByLevel() {
    //    Aggregation aggregation = newAggregation(
    //            group("wlin_protection_level").count().as("wetlandNum"),
    //            project("wetlandNum").and("level").previousOperation()
    //    );
    //
    //    //这里最好使用Document来接受聚合过的数据
    //    AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, COLLECTION, Document.class);
    //    List<Document> resultList = result.getMappedResults();
    //    log.info("根据level分类得到的保护主体个数聚合的结果:{}", resultList);
    //    //log.info(resultList.get(0).getString("level"));
    //    return resultList;
    //}
}
